package com.booktrade.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.booktrade.entity.Messages;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 私信表，存储用户之间的消息 Mapper 接口
 * </p>
 */
public interface MessagesMapper extends BaseMapper<Messages> {

    /**
     * 获取用户私聊过的所有唯一联系人ID列表
     *
     * @param userId 用户ID
     * @param sent   true表示查询发送的联系人ID，false表示查询接收的联系人ID
     * @return 私聊过的唯一联系人ID列表
     */
    @Select("<script>" +
            "SELECT DISTINCT " +
            "<if test='sent'>receiver_id</if>" +
            "<if test='!sent'>sender_id</if> " +
            "FROM messages " +
            "WHERE <if test='sent'>sender_id = #{userId}</if>" +
            "<if test='!sent'>receiver_id = #{userId}</if>" +
            "</script>")
    List<Integer> selectDistinctContactIds(@Param("userId") Integer userId, @Param("sent") boolean sent);
}

